<?php
/**
 * 报表管理
 * @author jianhui
 *
 */
class Report extends MY_Controller{
    var $reportType;
    function Report(){
        parent::MY_Controller();
        $fundList = $this->config->item('fundType');
        $this->fundList = $fundList;
        $this->reportList = array(
            1 => array(1=>'线上明细表', 2=>'线下明细表', 3=>'项目费用表', 4=>'标准动作执行统计表'),
            2 => array(1=>'线上明细表', 2=>'线下明细表', 3=>'项目费用表', 4=>'标准动作执行统计表'/* , 5=>'年度费用使用表' */),
            3 => array(1=>'线上明细表', 2=>'线下明细表', 3=>'项目费用表',),
            4 => array(1=>'线上明细表', 2=>'线下明细表', 3=>'项目费用表',),
            5 => array(1 => '固定传播项目', 2=> '双月发布检核',),
            6 => array(1=>'线上明细表', 2=>'线下明细表', 3=>'项目费用表',),
            7 => array(1=>'线上明细表', 2=>'线下明细表', 3=>'项目费用表',),
        );
        $this->load->model('timeUnitModel');
    }
    /**
     * 载入报表类型json数据
     */
    function loadReportList(){
        $fundType = (int)$this->input->post('fundType');
        require_once APPPATH.'libraries/JSON.php';
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if (isset($this->reportList[$fundType])){
            foreach ($this->reportList[$fundType] as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 报表管理首页
     */
    function index(){
        //查询条件
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位
        $timeUnit = (int)$this->input->get('timeUnit');
        $timeUnit || $timeUnit = $this->timeUnitModel->getNowTimeUnit();
        $this->viewData['timeUnit'] = $timeUnit;
        //是否为补报
        $isBubao = (int)$this->input->get('isBubao');
        //基金类型
        $fundType = (int)$this->input->get('fundType');
        //报表类型
        $reportType = (int)$this->input->get('reportType');
        if (isset($_GET['submit']) && $_GET['submit'] == 'export'){
            isset($this->reportList[$fundType][$reportType]) || showError('请选择正确的报表类型');
            if ($fundType == 2 && $timeUnit == 1 && !in_array($reportType, array(1, 2, 3))){
                showError('1-2月不存在此报表类型');
            }
            //条件参数
            $params = array('area'=>$area, 'region'=>$region, 'province'=>$province, 'city'=>$city, 'storeId'=>$storeId, 'keyword'=>$keyword, 'year'=>$year, 'timeUnit'=>$timeUnit, 'isBubao'=>$isBubao);
            //导出文件名
            $fileName = $this->fundList[$fundType].'-'.$this->reportList[$fundType][$reportType].'-'.$year.'年';
            //固定传播基金报表名称特殊处理
            if ($fundType != 5){
                if ($timeUnit){
                    $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
                    isset($timeUnitList[$year][$timeUnit]) && $fileName .= '-'.$timeUnitList[$year][$timeUnit];
                }
                $fileName .= '-'. ($isBubao == 1 ? '补报':'普通申报');
            }
            //设置excel文件下载头
            setExcelDownHeader($fileName);
            switch ($fundType){
                case 1://单店基金
                    $this->getSingleExcel($reportType, $params);
                    break;
                case 2://城市提升基金
                    if ($timeUnit == 1){//1-2月
                        $this->getCitySimpleExcel($reportType, $params);
                    }else{//3-12月
                        $this->getCityExcel($reportType, $params);
                    }
                    break;
                case 3://车展基金
                    $this->getCarshowExcel($reportType, $params);
                    break;
                case 4://巡展基金
                    $this->getXunExcel($reportType, $params);
                    break;
                case 5://固定传播基金
                    $this->getSpreadExcel($reportType, $params);
                    break;
                case 6://专项单店基金
                    $this->getSpecialExcel($reportType, $params);
                    break;
                case 7://专项城市基金
                    $this->getSpecityExcel($reportType, $params);
                    break;
            }
        }else{
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('Report', 'index'),'title'=>'报表管理');
            
            $this->load->view('Report/index', $this->viewData);
        }
    }
    /**
     * 单店报表初始化
     * @param int $reportType
     * @param array $params
     */
    function getSingleExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('singleProcessModel');
        $procList = $this->singleProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        //项目活动
        $this->load->model('singleProjectModel');
        $projectWhere = array('procId'=>array('in', $procIds), 'isBubao'=>$params['isBubao']);
        $projectList = $this->singleProjectModel->getNewList($projectWhere);
        $this->viewData['projectList'] = $projectList;
        switch ($reportType){
            case 1:
                $this->getSingleGxExcel($params);
                break;
            case 2:
                $this->getSingleOfflineExcel($params);
                break;
            case 3:
                $this->getSingleProjectExcel($params);
                break;
            case 4:
                $this->getSingleActionExcel($params);
                break;
        }
    }
    /**
     * 单店基金-线上明细表
     * @param array $params 筛选条件
     */
    function getSingleGxExcel($params){
        //项目活动-广宣
        $this->load->model('singlePGxModel');
        $pGxWhere = array('projectId' => array('in',array_keys($this->viewData['projectList'])));
        $pGxList = $this->singlePGxModel->getNewList($pGxWhere, NULL, NULL, '', 'projectId ASC');
        $pGxGroupList = array();
        foreach ($pGxList as $v){
            $groupKey = (int)$v['procId'];
            isset($pGxGroupList[$groupKey]) || $pGxGroupList[$groupKey] = array();
            $pGxGroupList[$groupKey][] = $v;
        }
        $this->viewData['pGxGroupList'] = $pGxGroupList;
        //天网行动
        $this->load->model('singleNetModel');
        $netWhere = array('procId' =>array('in', $this->viewData['procIds']),'isBubao'=>$params['isBubao']);
        $netList = $this->singleNetModel->getNewList($netWhere);
        $netGroupList = array();
        foreach ($netList as $v){
            $groupKey = (int)$v['procId'];
            isset($netList[$groupKey]) || $netList[$groupKey] = array();
            $netGroupList[$groupKey][] = $v;
        }
        $this->viewData['netGroupList'] = $netGroupList;
        $this->load->view('Report/singleGxExcel', $this->viewData);
    }
    /**
     * 单店基金-线下明细表
     * @param array $params 筛选条件
     */
    function getSingleOfflineExcel($params){
        //项目活动-线下活动
        $this->load->model('singlePOfflineModel');
        $pOfflineWhere = array('projectId'=>array('in', array_keys($this->viewData['projectList'])));
        $pOfflineList = $this->singlePOfflineModel->getNewList($pOfflineWhere, NULL, NULL, '', 'projectId ASC');
        $pOfflineGroupList = array();
        foreach ($pOfflineList as $v){
            $groupKey = (int)$v['procId'];
            isset($pOfflineGroupList[$groupKey]) || $pOfflineGroupList[$groupKey] = array();
            $pOfflineGroupList[$groupKey][] = $v;
        }
        $this->viewData['pOfflineGroupList'] = $pOfflineGroupList;
        //项目活动-线下活动-金额明细列表
        $this->load->model('singlePODetailModel');
        $pMoneyDetailList = $this->singlePODetailModel->getMoneyDetailList($pOfflineList);
        $this->viewData['pMoneyDetailList'] = $pMoneyDetailList;
        
        $this->load->view('Report/singleOfflineExcel', $this->viewData);
    }
    /**
     * 单店基金-项目费用表
     * @param array $params 筛选条件
     */
    function getSingleProjectExcel($params){
        //单店基金-分组列表
        $projectGroupList = array();
        foreach ($this->viewData['projectList'] as $v){
            $groupKey = (int)$v['procId'];
            isset($projectGroupList[$groupKey]) || $projectGroupList[$groupKey] = array();
            $projectGroupList[$groupKey][] = $v;
        }
        $this->viewData['projectGroupList'] = $projectGroupList;
        //单店基金-费用明细列表
        $this->viewData['projectMoneyDetail'] = $this->singleProjectModel->getMoneyDetailList($this->viewData['projectList']);
        //天网行动
        $this->load->model('singleNetModel');
        $netWhere = array('procId' =>array('in', $this->viewData['procIds']),'isBubao'=>$params['isBubao']);
        $netList = $this->singleNetModel->getNewList($netWhere);
        $netGroupList = array();
        foreach ($netList as $v){
            $groupKey = (int)$v['procId'];
            isset($netList[$groupKey]) || $netList[$groupKey] = array();
            $netGroupList[$groupKey][] = $v;
        }
        $this->viewData['netGroupList'] = $netGroupList;
        
        $this->load->view('Report/singleProjectExcel', $this->viewData);
    }
    /**
     * 单店基金-标准动作执行统计表
     * @param array $params 筛选条件
     */
    function getSingleActionExcel($params){
        //标准动作数据
        $this->load->model('actionModel');
        $actionList = $this->actionModel->getActiveList(1, $params['year'], $params['timeUnit']);
        $this->viewData['actionList'] = $actionList;
        //标准动作数据分析
        $actionDetailList = $this->singleProjectModel->explanAction($this->viewData['projectList'], $actionList);
        $this->viewData['actionDetailList'] = $actionDetailList;
        $this->load->view('Report/singleActionExcel', $this->viewData);
    }
    /**
     * 城市基金报表简化版（1-2月）
     * @param unknown $reportType
     * @param unknown $params
     */
    function getCitySimpleExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('cityProcessModel');
        $procList = $this->cityProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        
        switch ($reportType){
            case 1://线上明细表
                $this->getCitySimpleGxExcel($params);
                break;
            case 2://线上明细表
                $this->getCitySimpleOfflineExcel($params);
                break;
            case 3://项目费用表
                $this->getCitySimpleProjectExcel($params);
                break;
        }
    }
    /**
     * 城市基金线上明细表简化版（1-2月）
     * @param array $params
     */
    function getCitySimpleGxExcel($params){
        //常规同城活动-广宣
        $this->load->model('cityGxModel');
        $gxWhere = array('procId' => array('in', $this->viewData['procIds']),'isBubao'=>$params['isBubao']);
        $gxList = $this->cityGxModel->getNewList($gxWhere, NULL, NULL, '', 'procId ASC');
        $gxGroupList = array();
        foreach ($gxList as $v){
            $groupKey = (int)$v['procId'];
            isset($gxGroupList[$groupKey]) || $gxGroupList[$groupKey] = array();
            $gxGroupList[$groupKey][] = $v;
        }
        $this->viewData['gxGroupList'] = $gxGroupList;
        
        $this->load->view('Report/citySimpleGxExcel', $this->viewData);
    }
    /**
     * 城市基金线下明细表简化版（1-2月）
     * @param array $params
     */
    function getCitySimpleOfflineExcel($params){
        //常规同城活动-线下活动
        $this->load->model('cityOfflineModel');
        $offlineWhere = array('procId' => array('in', array_keys($this->viewData['procIds'])),'isBubao'=>$params['isBubao']);
        $offlineList = $this->cityOfflineModel->getNewList($offlineWhere, NULL, NULL, '', 'procId ASC');
        $offlineGroupList = array();
        foreach ($offlineList as $v){
            $groupKey = (int)$v['procId'];
            isset($offlineGroupList[$groupKey]) || $offlineGroupList[$groupKey] = array();
            $offlineGroupList[$groupKey][] = $v;
        }
        $this->viewData['offlineGroupList'] = $offlineGroupList;
        //常规同城活动-线下活动-费用明细列表
        $this->load->model('cityODetailModel');
        $offlineMoneyDetail = $this->cityODetailModel->getMoneyDetailList($offlineList);
        $this->viewData['offlineMoneyDetail'] = $offlineMoneyDetail;
        $this->load->view('Report/citySimpleOfflineExcel', $this->viewData);
    }
    /**
     * 城市基金项目费用表简化版（1-2月）
     * @param array $params
     */
    function getCitySimpleProjectExcel($params){
        //常规同城活动-金额明细列表
        $this->viewData['projectMoneyDetail'] = $this->cityProcessModel->getMoneyDetailList($this->viewData['procList'], $params['isBubao']);
        $this->load->view('Report/citySimpleProjectExcel', $this->viewData);
    }
    /**
     * 城市提升基金报表初始化（3-12月）
     * @param int $reportType 报表类型
     * @param array $params 筛选条件
     */
    function getCityExcel($reportType, $params) {
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('cityProcessModel');
        $procList = $this->cityProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        //常规同城活动
        $this->load->model('cityProjectModel');
        $projectWhere = array('procId'=>array('in', $procIds), 'isBubao'=>$params['isBubao']);
        $projectList = $this->cityProjectModel->getNewList($projectWhere);
        $this->viewData['projectList'] = $projectList;
    
        switch ($reportType){
            case 1://线上明细表
                $this->getCityGxExcel($params);
                break;
            case 2://线上明细表
                $this->getCityOfflineExcel($params);
                break;
            case 3://项目费用表
                $this->getCityProjectExcel($params);
                break;
            case 4://标准动作执行统计表
                $this->getCityActionExcel($params);
                break;
            case 5://年度费用使用表
                $this->getCityYearMoneyExcel($params);
                break;
        }
    }
    /**
     * 城市提升基金-线上明细表（3-12月）
     * @param array $params
     */
    function getCityGxExcel($params){
        //常规同城活动-广宣
        $this->load->model('cityGxModel');
        $gxWhere = array('projectId' => array('in', array_keys($this->viewData['projectList'])));
        $gxList = $this->cityGxModel->getNewList($gxWhere, NULL, NULL, '', 'projectId ASC');
        $gxGroupList = array();
        foreach ($gxList as $v){
            $groupKey = (int)$v['procId'];
            isset($gxGroupList[$groupKey]) || $gxGroupList[$groupKey] = array();
            $gxGroupList[$groupKey][] = $v;
        }
        $this->viewData['gxGroupList'] = $gxGroupList;
    
        $this->load->view('Report/cityGxExcel', $this->viewData);
    }
    /**
     * 城市提升基金-线下明细表（3-12月）
     * @param array $params
     */
    function getCityOfflineExcel($params){
        //常规同城活动-线下活动
        $this->load->model('cityOfflineModel');
        $offlineWhere = array('projectId' => array('in', array_keys($this->viewData['projectList'])));
        $offlineList = $this->cityOfflineModel->getNewList($offlineWhere, NULL, NULL, '', 'projectId ASC');
        $offlineGroupList = array();
        foreach ($offlineList as $v){
            $groupKey = (int)$v['procId'];
            isset($offlineGroupList[$groupKey]) || $offlineGroupList[$groupKey] = array();
            $offlineGroupList[$groupKey][] = $v;
        }
        $this->viewData['offlineGroupList'] = $offlineGroupList;
        //常规同城活动-线下活动-费用明细列表
        $this->load->model('cityODetailModel');
        $offlineMoneyDetail = $this->cityODetailModel->getMoneyDetailList($offlineList);
        $this->viewData['offlineMoneyDetail'] = $offlineMoneyDetail;
        
        $this->load->view('Report/cityOfflineExcel', $this->viewData);
    }
    /**
     * 城市提升基金-项目费用表（3-12月）
     * @param array $params
     */
    function getCityProjectExcel($params){
        //常规同城活动-分组
        $projectGroupList = array();
        foreach ($this->viewData['projectList'] as $v){
            $groupKey = (int)$v['procId'];
            isset($projectGroupList[$groupKey]) || $projectGroupList[$groupKey] = array();
            $projectGroupList[$groupKey][] = $v;
        }
        $this->viewData['projectGroupList'] = $projectGroupList;
        //常规同城活动-金额明细列表
        $this->viewData['projectMoneyDetail'] = $this->cityProjectModel->getMoneyDetailList($this->viewData['projectList']);
        
        $this->load->view('Report/cityProjectExcel', $this->viewData);
    }
    /**
     * 城市提升基金-标准动作执行统计表（3-12月）
     * @param array $params
     */
    function getCityActionExcel($params){
        //标准动作数据
        $this->load->model('actionModel');
        $actionList = $this->actionModel->getActiveList(3, $params['year'], $params['timeUnit']);
        $this->viewData['actionList'] = $actionList;
        //标准动作数据分析
        $actionDetailList = $this->cityProjectModel->explanAction($this->viewData['projectList'], $actionList);
        $this->viewData['actionDetailList'] = $actionDetailList;
        $this->load->view('Report/cityActionExcel', $this->viewData);
    }
    /**
     * 城市提升基金-年度费用使用表（3-12月）
     * @param array $params
     */
    function getCityYearMoneyExcel($params){
        //时段列表
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getTimeUnitList($params['year']);
        //年度额度列表
        $this->viewData['storeDetailList'] = $this->cityProcessModel->getYearTotalListByStoreId($params['year'], $params['timeUnit']);
        $this->load->view('Report/cityYearMoneyExcel', $this->viewData);
    }
    /**
     * 车展基金
     * @param int $reportType
     * @param array $params
     */
    function getCarshowExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('carProcessModel');
        $procList = $this->carProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        //地方车展
        $this->load->model('carProjectModel');
        $projectWhere = array('procId'=> array('in', $procIds), 'isBubao'=>$params['isBubao']);
        $projectList = $this->carProjectModel->getNewList($projectWhere);
        $this->viewData['projectList'] = $projectList;
        switch ($reportType){
            case 1://线上明细表
                $this->getCarshowGxExcel($params);
                break;
            case 2://线下明细表
                $this->getCarshowOfflineExcel($params);
                break;
            case 3://项目费用表
                $this->getCarshowProjectExcel($params);
                break;
        }
    }
    /**
     * 车展基金-线上明细表
     * @param array $params
     */
    function getCarshowGxExcel($params){
        //地方车展-广宣
        $this->load->model('carGxModel');
        $gxWhere = array('projectId'=>array('in', array_keys($this->viewData['projectList'])));
        $gxList = $this->carGxModel->getNewList($gxWhere, NULL, NULL, '', 'projectId ASC');
        $this->viewData['gxList'] = $gxList;
        
        $this->load->view('Report/carshowGxExcel', $this->viewData);
    }
    /**
     * 车展基金-线下明细表
     * @param array $params
     */
    function getCarshowOfflineExcel($params){
        $projectIds = array_keys($this->viewData['projectList']);
        $this->viewData['projectIds'] = $projectIds;
        //车展场租费用
        $this->load->model('carRentModel');
        $rentWhere = array('projectId'=>array('in', $projectIds));
        $rentList = $this->carRentModel->getNewList($rentWhere, NULL, NULL, '', 'projectId ASC');
        $rentGroupList = array();
        foreach ($rentList as $v){
            $groupKey = (int)$v['projectId'];
            isset($rentGroupList[$groupKey]) || $rentGroupList[$groupKey] = array();
            $rentGroupList[$groupKey][] = $v;
        }
        $this->viewData['rentGroupList'] = $rentGroupList;
        //场馆配套费用
        $this->load->model('carMatchingModel');
        $matchingWhere = array('projectId'=>array('in', $projectIds));
        $matchingList = $this->carMatchingModel->getNewList($matchingWhere, NULL, NULL, '', 'projectId ASC');
        $matchingGroupList = array();
        foreach ($matchingList as $v){
            $groupKey = (int)$v['projectId'];
            isset($matchingGroupList[$groupKey]) || $matchingGroupList[$groupKey] = array();
            $matchingGroupList[$groupKey][] = $v;
        }
        $this->viewData['matchingGroupList'] = $matchingGroupList;
        
        $this->load->view('Report/carshowOfflineExcel', $this->viewData);
    }
    /**
     * 车展基金-项目费用表
     * @param array $params
     */
    function getCarshowProjectExcel($params){
        //地方车展-费用明细列表
        $this->viewData['projectMoneyDetail'] = $this->carProjectModel->getMoneyDetailList($this->viewData['projectList']);
        
        $this->load->view('Report/carshowProjectExcel', $this->viewData);
    }
    /**
     * 巡展基金报表初始化
     * @param int $reportType 报表类型
     * @param array $params 筛选条件爱你
     */
    function getXunExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('xunProcessModel');
        $procList = $this->xunProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        //项目活动
        $this->load->model('xProjectModel');
        $projectWhere = array('procId'=>array('in', $procIds));
        $projectList = $this->xProjectModel->getNewList($projectWhere);
        $this->viewData['projectList'] = $projectList;
        switch ($reportType){
            case 1://线上明细表
                $this->getXunGxExcel($params);
                break;
            case 2://线下明细表
                $this->getXunOfflineExcel($params);
                break;
            case 3://项目费用表
                $this->getXunProjectExcel($params);
                break;
        }
    }
    /**
     * 巡展基金-线上明细表
     * @param array $params
     */
    function getXunGxExcel($params){
        //项目-广宣列表
        $this->load->model('xPGxModel');
        $pGxWhere = array('activityId' => array('in', array_keys($this->viewData['projectList'])));
        $pGxList = $this->xPGxModel->getNewList($pGxWhere, NULL, NULL, '', 'activityId ASC');
        $this->viewData['pGxList'] = $pGxList;
        $this->load->view('Report/xunGxExcel', $this->viewData);
    }
    /**
     * 巡展基金-线下活动列表
     * @param array $params
     */
    function getXunOfflineExcel($params){
        //项目-线下活动列表
        $this->load->model('xPOfflineModel');
        $pOfflineWhere = array('activityId' => array('in', array_keys($this->viewData['projectList'])));
        $pOfflineList = $this->xPOfflineModel->getNewList($pOfflineWhere, NULL, NULL, '', 'activityId ASC');
        $this->viewData['pOfflineList'] = $pOfflineList;
        //项目-线下活动金额明细
        $this->load->model('xPODetailModel');
        $moneyDetailList = $this->xPODetailModel->getMoneyDetailList($pOfflineList);
        $this->viewData['moneyDetailList'] = $moneyDetailList;
        $this->load->view('Report/xunOfflineExcel', $this->viewData);
    }
    /**
     * 巡展基金-项目费用表
     * @param array $params
     */
    function getXunProjectExcel($params){
        /* $moneyDetailList = $this->xProjectModel->getMoneyDetailList($this->viewData['projectList']);
        $this->viewData['moneyDetailList'] = $moneyDetailList; */
        $this->load->view('Report/xunProjectExcel', $this->viewData);
    }
    /**
     * 固定传播基金-报表初始化
     * @param number $reportType
     * @param array $params
     */
    function getSpreadExcel($reportType, $params){
        
        switch ($reportType){
            case 1://线上明细表
                $this->getSpreadProjectExcel($params);
                break;
            case 2://线下明细表
                $this->getSpreadDbmonthExcel($params);
        }
    }
    /**
     * 固定传播基金-固定传播项目报表
     * @param array $params
     */
    function getSpreadProjectExcel($params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        //$procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('spreadProcessModel');
        $procList = $this->spreadProcessModel->getNewList($procWhere, NULL, NULL, '', 'belongProcId ASC');
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        //专营店列表
        $this->load->model('spreadProcStoreModel');
        $groupByProcIdList = $this->spreadProcStoreModel->getProcStoreListByProcId($procIds);
        $this->viewData['groupByProcIdList'] = $groupByProcIdList;
        
        //地方车展
        $this->load->model('spreadProjectModel');
        $projectWhere = array('procId'=> array('in', $this->viewData['procIds']));
        $projectList = $this->spreadProjectModel->getNewList($projectWhere);
        $this->viewData['projectList'] = $projectList;
        
        $this->load->view('Report/spreadProjectExcel', $this->viewData);
    }
    /**
     * 固定传播基金-双月发布检核报表
     * @param unknown $params
     */
    function getSpreadDbmonthExcel($params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['auditRs'] = 1;
        $this->load->model('spreadProcessModel');
        $procList = $this->spreadProcessModel->getNewList($procWhere, NULL, NULL, '', 'belongProcId ASC');
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        //地方车展
        $this->load->model('spreadProjectModel');
        $projectWhere = array('procId'=> array('in', $this->viewData['procIds']));
        $projectList = $this->spreadProjectModel->getNewList($projectWhere);
        $this->viewData['projectList'] = $projectList;
        //项目双月发布检核
        $this->load->model('spreadProItemModel');
        $proItemList = $this->spreadProItemModel->getNewList(array('projectId'=>array('in', array_keys($this->viewData['projectList']))));
        $proItemGroupList = array();
        foreach ($proItemList as $v){
            $groupKey = (int)$v['projectId'];
            isset($proItemGroupList[$groupKey]) || $proItemGroupList[$groupKey] = array();
            $proItemGroupList[$groupKey][(int)$v['timeUnit']] = $v;
        }
        $this->viewData['proItemGroupList'] = $proItemGroupList;
        //固定传播基金时段列表
        $timeUnitList = $this->timeUnitModel->getSpreadDbmonthTimeUnitList();
        $this->viewData['timeUnitList'] = $timeUnitList;
        
        $this->load->view('Report/spreadDbmonthExcel', $this->viewData);
    }
    /**
     * 专项单店基金报表初始化
     * @param number $reportType
     * @param array $params
     */
    function getSpecialExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('specialProcessModel');
        $procList = $this->specialProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        //项目活动
        $this->load->model('specialProjectModel');
        $projectWhere = array('procId'=>array('in', $procIds), 'isBubao'=>$params['isBubao']);
        $projectList = $this->specialProjectModel->getNewList($projectWhere);
        $this->viewData['projectList'] = $projectList;
        switch ($reportType){
            case 1:
                $this->getSpecialGxExcel($params);
                break;
            case 2:
                $this->getSpecialOfflineExcel($params);
                break;
            case 3:
                $this->getSpecialProjectExcel($params);
                break;
        }
    }
    /**
     * 专项单店基金-线上明细表
     * @param array $params 筛选条件
     */
    function getSpecialGxExcel($params){
        //项目活动-广宣
        $this->load->model('specialPGxModel');
        $pGxWhere = array('projectId' => array('in',array_keys($this->viewData['projectList'])));
        $pGxList = $this->specialPGxModel->getNewList($pGxWhere, NULL, NULL, '', 'projectId ASC');
        $pGxGroupList = array();
        foreach ($pGxList as $v){
            $groupKey = (int)$v['procId'];
            isset($pGxGroupList[$groupKey]) || $pGxGroupList[$groupKey] = array();
            $pGxGroupList[$groupKey][] = $v;
        }
        $this->viewData['pGxGroupList'] = $pGxGroupList;
        $this->load->view('Report/specialGxExcel', $this->viewData);
    }
    /**
     * 专项单店基金-线下明细表
     * @param array $params 筛选条件
     */
    function getSpecialOfflineExcel($params){
        //项目活动-线下活动
        $this->load->model('specialPOfflineModel');
        $pOfflineWhere = array('projectId'=>array('in', array_keys($this->viewData['projectList'])));
        $pOfflineList = $this->specialPOfflineModel->getNewList($pOfflineWhere, NULL, NULL, '', 'projectId ASC');
        $pOfflineGroupList = array();
        foreach ($pOfflineList as $v){
            $groupKey = (int)$v['procId'];
            isset($pOfflineGroupList[$groupKey]) || $pOfflineGroupList[$groupKey] = array();
            $pOfflineGroupList[$groupKey][] = $v;
        }
        $this->viewData['pOfflineGroupList'] = $pOfflineGroupList;
        //项目活动-线下活动-金额明细列表
        $this->load->model('specialPODetailModel');
        $pMoneyDetailList = $this->specialPODetailModel->getMoneyDetailList($pOfflineList);
        $this->viewData['pMoneyDetailList'] = $pMoneyDetailList;
    
        $this->load->view('Report/specialOfflineExcel', $this->viewData);
    }
    /**
     * 专项单店基金-项目费用表
     * @param array $params 筛选条件
     */
    function getSpecialProjectExcel($params){
        //单店基金-分组列表
        $projectGroupList = array();
        foreach ($this->viewData['projectList'] as $v){
            $groupKey = (int)$v['procId'];
            isset($projectGroupList[$groupKey]) || $projectGroupList[$groupKey] = array();
            $projectGroupList[$groupKey][] = $v;
        }
        $this->viewData['projectGroupList'] = $projectGroupList;
        //单店基金-费用明细列表
        $this->viewData['projectMoneyDetail'] = $this->specialProjectModel->getMoneyDetailList($this->viewData['projectList']);
        
        $this->load->view('Report/specialProjectExcel', $this->viewData);
    }
    /**
     * 专项城市基金报表初始化
     * @param number $reportType
     * @param array $params
     */
    function getSpecityExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('specityProcessModel');
        $procList = $this->specityProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        //项目活动
        $this->load->model('specityProjectModel');
        $projectWhere = array('procId'=>array('in', $procIds), 'isBubao'=>$params['isBubao']);
        $projectList = $this->specityProjectModel->getNewList($projectWhere);
        $this->viewData['projectList'] = $projectList;
        switch ($reportType){
            case 1:
                $this->getSpecityGxExcel($params);
                break;
            case 2:
                $this->getSpecityOfflineExcel($params);
                break;
            case 3:
                $this->getSpecityProjectExcel($params);
                break;
        }
    }
    /**
     * 专项城市基金-线上明细表
     * @param array $params 筛选条件
     */
    function getSpecityGxExcel($params){
        //项目活动-广宣
        $this->load->model('specityPGxModel');
        $pGxWhere = array('projectId' => array('in',array_keys($this->viewData['projectList'])));
        $pGxList = $this->specityPGxModel->getNewList($pGxWhere, NULL, NULL, '', 'projectId ASC');
        $pGxGroupList = array();
        foreach ($pGxList as $v){
            $groupKey = (int)$v['procId'];
            isset($pGxGroupList[$groupKey]) || $pGxGroupList[$groupKey] = array();
            $pGxGroupList[$groupKey][] = $v;
        }
        $this->viewData['pGxGroupList'] = $pGxGroupList;
        $this->load->view('Report/specityGxExcel', $this->viewData);
    }
    /**
     * 专项城市基金-线下明细表
     * @param array $params 筛选条件
     */
    function getSpecityOfflineExcel($params){
        //项目活动-线下活动
        $this->load->model('specityPOfflineModel');
        $pOfflineWhere = array('projectId'=>array('in', array_keys($this->viewData['projectList'])));
        $pOfflineList = $this->specityPOfflineModel->getNewList($pOfflineWhere, NULL, NULL, '', 'projectId ASC');
        $pOfflineGroupList = array();
        foreach ($pOfflineList as $v){
            $groupKey = (int)$v['procId'];
            isset($pOfflineGroupList[$groupKey]) || $pOfflineGroupList[$groupKey] = array();
            $pOfflineGroupList[$groupKey][] = $v;
        }
        $this->viewData['pOfflineGroupList'] = $pOfflineGroupList;
        //项目活动-线下活动-金额明细列表
        $this->load->model('specityPODetailModel');
        $pMoneyDetailList = $this->specityPODetailModel->getMoneyDetailList($pOfflineList);
        $this->viewData['pMoneyDetailList'] = $pMoneyDetailList;
    
        $this->load->view('Report/specityOfflineExcel', $this->viewData);
    }
    /**
     * 专项城市基金-项目费用表
     * @param array $params 筛选条件
     */
    function getSpecityProjectExcel($params){
        //单店基金-分组列表
        $projectGroupList = array();
        foreach ($this->viewData['projectList'] as $v){
            $groupKey = (int)$v['procId'];
            isset($projectGroupList[$groupKey]) || $projectGroupList[$groupKey] = array();
            $projectGroupList[$groupKey][] = $v;
        }
        $this->viewData['projectGroupList'] = $projectGroupList;
        //单店基金-费用明细列表
        $this->viewData['projectMoneyDetail'] = $this->specityProjectModel->getMoneyDetailList($this->viewData['projectList']);
    
        $this->load->view('Report/specityProjectExcel', $this->viewData);
    }
}